const state = {
  visitedViews: []
}
const mutations = {
  // 增加路由
  ADD_VISITED_VIEW(state, view) {
    // 1、判断visitedview中是否包含当前路由
    // 如果包含则不添加
    if (state.visitedViews.some(item => item.path === view.path)) {
      return
    }
    // 如果不包含
    state.visitedViews.push(
      Object.assign({}, view)
    )
  },
  // 删除路由
  DEL_SELECT_VIEW(state, view) {
    for (const [i, route] of state.visitedViews.entries()) {
      if (route.path === view.path) {
        state.visitedViews.splice(i, 1)
        break
      }
    }
  }
}
const actions = {
  // 增加路由action
  addVisitedView({ commit }, view) {
    commit('ADD_VISITED_VIEW', view)
  },
  // 删除路由action
  delSelectView({ commit }, view) {
    // commit('DEL_SELECT_VIEW', view)
    return new Promise(resolve => {
      commit('DEL_SELECT_VIEW', view)
      resolve([...state.visitedViews])
    })
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
